import 'package:flutter_ahj_app/app/my/invoicing/HttpConfiguration/FP_HttpRequest.dart';
import 'package:flutter_ahj_app/app/my/invoicing/fpHeader/bean/FP_PresetInvoice_Id_Bean.dart';
import 'package:flutter_ahj_app/base/basewidget/model/BaseModel.dart';
import 'package:rxdart/subjects.dart';

class FPHeaderModel extends BaseModel {
  //保存
  BehaviorSubject _saveSubject;
  Stream get saveStream => _saveSubject.stream;
  postSaveEvent({var params}) {
    _saveSubject = BehaviorSubject();
    FPHttpRequest.postPresetInvoice(params: params).listen((event) {
      print(event);
      _saveSubject.add(event);
    }, onError: (e) {
      errorData.add(e);
    });
  }

// 删除
  BehaviorSubject _deleteSubject;
  Stream get deleteStream => _deleteSubject.stream;
  deleteEvent(int id) {
    _deleteSubject = BehaviorSubject();
    FPHttpRequest.deletePresetInvoiceId(id).listen((event) {
      print(event);
      _deleteSubject.add(event);
    }, onError: (e) {
      errorData.add(e);
    });
  }

  // 获取信息
  BehaviorSubject<FPPresetInvoiceIdBean> _getDataSubject;
  Stream<FPPresetInvoiceIdBean> get getDataStream => _getDataSubject.stream;
  getDataEvent(int id) {
    _getDataSubject = BehaviorSubject();
    FPHttpRequest.getPresetInvoiceId(id).listen((event) {
      FPPresetInvoiceIdBean fpPresetInvoiceIdBean =
          FPPresetInvoiceIdBean.fromJson(event);
      print(event);
      _getDataSubject.add(fpPresetInvoiceIdBean);
    }, onError: (e) {
      errorData.add(e);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    if (_saveSubject != null) {
      _saveSubject.close();
    }
    if (_deleteSubject != null) {
      _deleteSubject.close();
    }
    if (_getDataSubject != null) {
      _getDataSubject.close();
    }
    super.dispose();
  }
}
